﻿using System;
using System.Linq;
using System.Collections.Generic;

namespace OrderManagement
{
    public class Order:IComparable
    {
        public int Ono { get; set; }//订单号
        public string Gname { get; set; }//商品名
        public string Customer { get; set; }//客户

        public double Oamount { get; set; }//订单金额
        public Order()
        {
            this.Ono = 0;
            this.Gname = string.Empty;
            this.Customer = string.Empty;
            this.Oamount = 0;
        }
        public Order(int onos,string gnames,string customers)
        {
            this.Ono = onos;
            this.Gname = gnames;
            this.Customer = customers;
        }
        public List<OrderDetails> orders = new List<OrderDetails>();
        public int CompareTo(object o)
        {
            Order item = o as Order;
            return this.Ono.CompareTo(item.Ono);

        }
        public override bool Equals(object o)
        {
            Order item = o as Order;
            return this.Ono == item.Ono;
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }
        public void theWholePrice()
        {
            double sum = 0;
            foreach(OrderDetails n in this.orders)
            {
                sum += n.getGAmount();
            }
            this.Oamount = sum;
        }
        public void ToString1()//展示订单明细
        {
            Console.WriteLine("每行从前往后分别为每一项商品的订单编号，商品名，数量和单价");
            for(int i = 0; i < orders.Count; i++)
            {
                Console.WriteLine("单内编号:{0},商品名:{1},数量:{2},单价:{3}", this.orders[i].OId, this.orders[i].Goodname, this.orders[i].GoodNum, this.orders[i].UnitPrice);
            }
        }
        public void AddDetails(int id, string name, int num, double price)
        {
            bool isEqual = false;
            OrderDetails i = new OrderDetails(id, name, num, price);
            foreach (OrderDetails o in this.orders)
            {
                if (i.Equals(o))
                {
                    isEqual = true;
                    Console.WriteLine("订单内编号重复！");
                }
            }
            if (!isEqual)
            {
                this.orders.Add(i);
                Console.WriteLine("添加成功");
            }
        }
        public void ChangeDetails() 
        {
            try
            {
                int m, n;
                Console.WriteLine("输入要改的订单明细单内编号（添加新明细输入任意整数都可）：");
                m = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("修改商品名输入1，修改商品数量输入2，修改商品单价输入3,添加明细输入4：");
                n = Convert.ToInt32(Console.ReadLine());
                switch (n)
                {
                    case 1:
                        Console.WriteLine("输入改后商品名：");
                        this.orders.ForEach(i =>
                        {
                            if (i.OId == m)
                            {
                                i.Goodname = Console.ReadLine();
                            }
                        }
                        );
                        break;
                    case 2:
                        Console.WriteLine("输入改后的数量：");
                        this.orders.ForEach(i =>
                        {
                            if (i.OId == m)
                            {
                                i.GoodNum = Convert.ToInt32(Console.ReadLine());
                            }
                        }
                        );
                        break;
                    case 3:
                        Console.WriteLine("输入改后单价：");
                        this.orders.ForEach(i =>
                        {
                            if (i.OId == m)
                            {
                                i.UnitPrice = Convert.ToDouble(Console.ReadLine());
                            }
                        }
                        );
                        break;
                    case 4:
                        int nid, nnum;
                        string gn;
                        double up;
                        Console.WriteLine("输入要添加的明细单内编号：");
                        nid = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("输入商品名");
                        gn = Console.ReadLine();
                        Console.WriteLine("输入商品数量：");
                        nnum = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("输入单价：");
                        up = Convert.ToDouble(Console.ReadLine());
                        this.AddDetails(nid, gn, nnum, up);
                        break;
                }
            }
            catch
            {
                Console.WriteLine("Error!请检查是否输入错误的单内编号/修改选项/不符修改项类型的数据");
            }
        }   
        public void DeleteDatails()
        {
            try
            {
                int m;
                Console.WriteLine("请输入要删除明细项的单内编号：");
                m = Convert.ToInt32(Console.ReadLine());
                for(int i = this.orders.Count - 1; i >= 0; i--)
                {
                    if(this.orders[i].OId == m)
                    {
                        this.orders.Remove(this.orders[i]);
                    }
                }
                Console.WriteLine("已删除");
            }
            catch
            {
                Console.WriteLine("Error!是否输入了不存在/错误的单内编号？");
            }
        }
        public void SortDetails()
        {
            Console.WriteLine("输入1/2/3分别按默认单内编号排序/商品数量/商品单价排序");
            int selec1 = Convert.ToInt32(Console.ReadLine());
            switch (selec1)
            {
                case 1:
                    this.orders.Sort();
                    break;
                case 2:
                    List<OrderDetails> sortDetails = this.orders.OrderBy(s => s.GoodNum).ToList<OrderDetails>();
                    this.orders = sortDetails;
                    break;
                case 3:
                    List<OrderDetails> sortDetails2 = this.orders.OrderBy(s => s.UnitPrice).ToList<OrderDetails>();
                    this.orders = sortDetails2;
                    break;
                default:
                    Console.WriteLine("Error!是否输入错误？");
                    break;
            }
        }
       
    }

   public class OrderDetails
   {
        private int orderid;
        public int OId
        {
            get
            {
                return orderid;
            }
            set
            {
                orderid = value;
            }
        }
        private string goodname;
        public string Goodname
        {
            get
            {
                return goodname;
            }
            set
            {
                goodname = value;
            }
        }
        private int goodnum;

        public int GoodNum
        {
            get
            {
                return goodnum;

            }
            set
            {
                goodnum = value;
            }
        }
        private double unitprice;//单价
        public double UnitPrice
        {
            get
            {
                return unitprice;
            }
            set
            {
                unitprice = value;
            }

        }
        public OrderDetails()
        {
            this.orderid = 0;
            this.goodname = string.Empty;
            this.goodnum = 0;
            this.unitprice = 0;
        }
        public OrderDetails(int oid,string Gname, int Gnum, double Unitprice)
        {
            this.orderid = oid;
            this.goodname = Gname;
            this.goodnum = Gnum;
            this.unitprice = Unitprice;
        }
        public double getGAmount()
        {
            double Amount = this.goodnum * this.unitprice;
            return Amount;
        }
        public int CompareTo(object o)
        {
            OrderDetails item = o as OrderDetails;
            return this.OId.CompareTo(item.OId);
        }
        public override bool Equals(object o)
        {
            OrderDetails item = o as OrderDetails;
            return this.OId == item.OId;
        }
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }





    }

    class OrderService
    {
        public List<Order> orderData = new List<Order>();
        public OrderService() { }
        public void AddOrder()
        {
            try
            {
                int onos;
                string gnames, customers;
                bool runAdd = true;
                bool equal = false;
                int contiAdd;
                string gname;
                int oid, num;
                double price;
                Console.WriteLine("输入订单号：");
                onos = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("输入订单中所有商品名：");
                gnames = Console.ReadLine();
                Console.WriteLine("输入下单客户名：");
                customers = Console.ReadLine();
                Order o = new Order(onos, gnames, customers);
                Console.WriteLine("输入订单明细：");
                    foreach (Order i in this.orderData)
                    {
                        if (i.Equals(o))
                        {
                            Console.WriteLine("请不要创建订单号重复的订单");
                            equal = true;
                        }
                    }
                if (!equal)
                {
                    
                    while (runAdd && !equal)
                    {
                        Console.WriteLine("输入单内编号：");
                        oid = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("输入商品名：");
                        gname = Console.ReadLine();
                        Console.WriteLine("请输入数量：");
                        num = Convert.ToInt32(Console.ReadLine());
                        Console.WriteLine("请输入其单价：");
                        price = Convert.ToDouble(Console.ReadLine());
                        o.AddDetails(oid, gname, num, price);
                        Console.WriteLine("还有明细项添加吗？输入1继续添加，输入其他任意整数退出");
                        contiAdd = Convert.ToInt32(Console.ReadLine());
                        if (contiAdd == 1)
                        {
                            continue;
                        }
                        else
                        {
                            runAdd = false;
                        }

                    }

                    orderData.Add(o);
                    o.theWholePrice();
                    Console.WriteLine("订单添加成功");
                }

                                    
            }
            catch
            {
                Console.WriteLine("Error!请检查输入类型是否正确等");
            }
        }
        public void DeleteOrder()
        {
            try
            {
                int dono;

                Console.WriteLine("请输入待删除项订单号");
                dono = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("删除整个订单输入1，仅删除某项明细输入2");
                int deleteWhich = Convert.ToInt32(Console.ReadLine());
                switch (deleteWhich)
                {
                    case 1:
                        for(int i = this.orderData.Count - 1; i >= 0; i--)
                        {
                            if(this.orderData[i].Ono == dono)
                            {
                                this.orderData.Remove(this.orderData[i]);
                            }
                        }
                        Console.WriteLine("已删除");
                        break;
                    case 2:
                        for (int i = this.orderData.Count - 1; i >= 0; i--)
                        {
                            if (this.orderData[i].Ono == dono)
                            {
                                this.orderData[i].DeleteDatails();
                            }
                        }
                        break;
                }

            }
            catch
            {
                Console.WriteLine("Error!请检查是否输入错误。");

            }

        }
        public void ToString2()//展示订单信息
        {
            for (int i = 0; i < orderData.Count; i++)
            {
                Console.WriteLine("订单号: {0} ,包含商品名: {1} ,下单客户名: {2} ,总金额 : {3} ", this.orderData[i].Ono, this.orderData[i].Gname, this.orderData[i].Customer, this.orderData[i].Oamount);
                this.orderData[i].ToString1();
            }

        }

        public void ChangeOrder()
        {
            try
            {
                int m, n;
                Console.WriteLine("输入要改的订单的订单号：");
                m = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("修改所含商品名输入1，修改下单客户名输入2，修改其中订单明细输入3：");
                n = Convert.ToInt32(Console.ReadLine());
                switch (n)
                {
                    case 1:
                        Console.WriteLine("输入改后商品名：");
                        this.orderData.ForEach(i =>
                        {
                            if (i.Ono == m)
                            {
                                i.Gname = Console.ReadLine();
                                Console.WriteLine("修改成功！");
                            }
                        }
                        );
                        break;
                    case 2:
                        Console.WriteLine("输入改后客户名：");
                        this.orderData.ForEach(i =>
                        {
                            if (i.Ono == m)
                            {
                                i.Customer = Console.ReadLine();
                                Console.WriteLine("修改成功！");
                            }
                        }
                        );
                        break;
                    case 3:
                        this.orderData.ForEach(i =>
                        {
                            if (i.Ono == m)
                            {
                                i.ChangeDetails();
                                i.theWholePrice();
                                Console.WriteLine("修改成功！");
                            }
                        }
                        );
                        
                        break;


                }
            }
            catch
            {
                Console.WriteLine("Error!请检查订单号是否错误/是否输入不存在选项等");
            }
        }
        public void searchOrderByLinq(int n)
        {
            try { 
            double MaxOamount, MinOamount;
            string custoname;
                string gName;
            int orderno;
                switch (n)
                {
                    case 1: Console.WriteLine("输入要查询的订单号：");
                        orderno = Convert.ToInt32(Console.ReadLine());
                        var query1 = from Sel1 in orderData
                                     where Sel1.Ono == orderno
                                     orderby Sel1.Oamount
                                     select Sel1;
                        List<Order> sel1 = query1.ToList();
                        Console.WriteLine("符合条件的订单：");
                        for (int i = 0; i < sel1.Count; i++)
                        {
                            Console.WriteLine("订单号:{0},商品名:{1},客户名:{2},总金额:{3} ",sel1[i].Ono, sel1[i].Gname, sel1[i].Customer, sel1[i].Oamount);
                            sel1[i].ToString1();
                        }
                        break;
                    case 2:Console.WriteLine("输入包含商品名：");
                        gName = Console.ReadLine();
                        var query2 = from Sel2 in orderData
                                     where Sel2.Gname == gName
                                     orderby Sel2.Oamount
                                     select Sel2;
                        List<Order> sel2 = query2.ToList();
                        Console.WriteLine("符合条件的订单：");
                        for(int i = 0;i< sel2.Count; i++)
                        {
                            Console.WriteLine("订单号:{0},商品名:{1},客户名:{2},总金额:{3}",sel2[i].Ono,sel2[i].Gname,sel2[i].Customer,sel2[i].Oamount);
                            sel2[i].ToString1();

                        }
                        break;
                    case 3:Console.WriteLine("输入查询订单的客户名：");
                        custoname = Console.ReadLine();
                        var query3 = from Sel3 in orderData
                                     where Sel3.Customer == custoname
                                     orderby Sel3.Oamount
                                     select Sel3;
                        List<Order> sel3 = query3.ToList();
                        Console.WriteLine("符合条件的订单：");
                        for(int i = 0; i < sel3.Count; i++)
                        {
                            Console.WriteLine("订单号:{0},商品名:{1},客户名:{2},总金额:{3}", sel3[i].Ono, sel3[i].Gname, sel3[i].Customer, sel3[i].Oamount);
                            sel3[i].ToString1();

                        }
                        break;
                    case 4:
                        Console.WriteLine("输入查询订单的金额范围：");
                        Console.WriteLine("最大：");
                        MaxOamount = Convert.ToDouble(Console.ReadLine());
                        Console.WriteLine("最小:");
                        MinOamount = Convert.ToDouble(Console.ReadLine());
                        var query4_1 = from Sel4 in orderData
                                       where Sel4.Oamount < MaxOamount
                                       orderby Sel4.Oamount
                                       select Sel4;
                        var query4_2 = from Sel5 in query4_1
                                       where MinOamount < Sel5.Oamount
                                       orderby Sel5.Oamount
                                       select Sel5;
                        List<Order> sel5 = query4_2.ToList();
                        Console.WriteLine("符合条件的订单：");
                        for (int i = 0; i < sel5.Count; i++)
                        {
                            Console.WriteLine("订单号:{0},商品名:{1},客户名:{2},总金额:{3}", sel5[i].Ono, sel5[i].Gname, sel5[i].Customer, sel5[i].Oamount);

                            sel5[i].ToString1();

                        }
                        break;



                }
            }
            catch
            {
                Console.WriteLine("Error!请检查是否输入错误数据");
            }
        }
    }



    class Program
    {
        static void Main(string[] args)
        {
            OrderService service = new OrderService();
            bool run = true;
            while (run)
            {
                Console.WriteLine("增：输入‘增’ 删：输入‘删’ 改：输入‘改’ 查：输入‘查’ 排序：输入‘排序’ 输出：输入‘输出’显示订单信息 退出：输入‘退出’");
                string sel = Console.ReadLine();
                switch (sel)
                {
                    case "增":
                        service.AddOrder();
                        break;
                    case "删":
                        service.DeleteOrder();
                        break;
                    case "改":
                        service.ChangeOrder();
                        break;
                    case "查":
                        Console.WriteLine("输入1/2/3/4可分别按订单号/商品名/客户名/总金额查询订单。");
                        int sele1 = Convert.ToInt32(Console.ReadLine());
                        service.searchOrderByLinq(sele1);
                        break;
                    case "排序":
                        Console.WriteLine("输入1/2/3可分别:默认按订单号排序/按订单金额排序/对订单中订单明细进行排序");
                        int sele2 = Convert.ToInt32(Console.ReadLine());
                        switch (sele2) {
                            case 1:
                                service.orderData.Sort();
                                break;
                            case 2:
                                List<Order> sortOrder = service.orderData.OrderBy(s => s.Oamount).ToList<Order>();
                                service.orderData = sortOrder;
                                break;
                            case 3:
                                Console.WriteLine("请输入要对明细排序的订单号");
                                int sono = Convert.ToInt32(Console.ReadLine());
                                service.orderData.ForEach(i =>
                                {
                                    if (i.Ono == sono)
                                    {
                                        i.SortDetails();
                                    }
                                }
                                );
                                break;
                            default:
                                Console.WriteLine("Error!是否输入错误？");
                                break;
                        }
                
                        break;
                    case "输出":
                        service.ToString2();
                        break;
                    case "退出":
                        run = false;
                        break;
                    default:
                        Console.WriteLine("Error!请检查是否输入错误！");
                        break;

                }
            }
        }
    }
}
